package com.example.huazong.myapplication.bottomnavigation;

import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.huazong.myapplication.R;


/**
 * Created by huazong on 2021/6/7.
 */

public class VideoActivity extends Fragment{
    TextView txt_connect;
    VideoView video;
    Spinner spinner;
    ImageButton imv;
    String Videodata[]={"影片一","哈利波特与混血王子","哈利波特与阿兹卡班的囚徒","数码宝贝"};
//    String Videodata[]={"哈利波特与混血王子","哈利波特与阿兹卡班的囚徒","哥斯拉2:怪兽之王"};


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.video, null);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取组件
        video = (VideoView) view.findViewById(R.id.video);
        spinner = (Spinner) view.findViewById(R.id.spinner);
        imv = (ImageButton) view.findViewById(R.id.imv);
        txt_connect=(TextView)view.findViewById(R.id.txt_connect);

       //设置适配器
        ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_dropdown_item_1line, Videodata);
        spinner.setAdapter(arrayAdapter);

        imv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isPlaying = video.isPlaying();
                if (isPlaying) {
                    video.pause();
                    imv.setImageResource(android.R.drawable.ic_media_play);
                } else {
                    video.start();
                    imv.setImageResource(android.R.drawable.ic_media_pause);
                }

            }
        });
        video.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                boolean isplaying = video.isPlaying();
                if (isplaying) {
                    video.pause();
                } else {
                    video.start();
                }
                return false;
            }
        });
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int id, long l) {
                switch (id) {
                    case 0:
                        VideoOn();
                        break;
                    case 1:
                        VideoOn1();
                        String current="食死徒开始肆无忌惮横行于麻瓜和魔法两个世界中，霍格沃茨内忧外患，危机重重。邓不利多带着哈利辗转找到隐居多年的霍拉斯·斯拉格霍恩，请他出山接替斯内普担任魔药学的老师。霍拉斯曾经教授过无数优秀的学生，而汤姆·里德尔——伏地魔——正是他最出色的一名弟子。邓不利多深信霍拉斯保有伏地魔不死身的秘密，他希望哈利能够说服霍拉斯。另一方面，德拉科·马尔福宣誓效忠伏地魔，他想方设法将食死徒引入霍格沃茨。而此时，罗恩、赫敏他们却还沉浸在青春的躁动中，全然不知黑暗正渐渐逼近……";
                        txt_connect.setText(current);
                        break;
                    case 2:
                        String connect="哈利即将在霍格沃兹渡过第三个年头，此时在阿兹卡班却传出小天狼星越狱的消息。据说小天狼星正是背叛哈利父母的好友，他的教父，而这次小天狼星越狱似乎正是为了找他。哈利的心里悄悄的滋生了为父母报仇的想法，期待着小天狼星的出现。新来的魔法老师卢平有着桀骜不驯的个性，与哈利关系很好，教了哈利许多实用的黑魔法防御知识。而此时在他的魔法地图上却出现了一个神秘人物，虫尾巴。偶然的机会下，在尖叫棚屋里集齐了哈利父亲当年的所有好友，卢平、虫尾巴、小天狼星，哈利的魔杖直指向小天狼星，但他发现那罪犯看他的眼神里充满了疼爱……";
                        txt_connect.setText(connect);
                        VideoOn2();
                        break;
                    case 3:
                        VideoOn3();
                        txt_connect.setText("");
                        break;

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void VideoOn3() {
        String sd_path= Environment.getExternalStorageDirectory().getAbsolutePath();//获取SD卡路径
        String path=sd_path+"/Movies/";
        video.setVideoPath(path+"数码宝贝.mp4");
        MediaController mediaController = new MediaController(getContext());
        video.setMediaController(mediaController);
        mediaController.setMediaPlayer(video);
    }

    private void VideoOn() {
        String path="android.resource://"+getActivity().getPackageName()+"/"+R.raw.testvideo;
        video.setVideoPath(path);
        MediaController mediaController = new MediaController(getContext());
        video.setMediaController(mediaController);
        mediaController.setMediaPlayer(video);
    }

    private void VideoOn1() {
        String sd_path= Environment.getExternalStorageDirectory().getAbsolutePath();//获取SD卡路径
        String path=sd_path+"/Movies/";
        video.setVideoPath(path+"哈利波特与混血王子.mp4");
            MediaController mediaController = new MediaController(getContext());
            video.setMediaController(mediaController);
            mediaController.setMediaPlayer(video);

        }
    private void VideoOn2() {
        String sd_path= Environment.getExternalStorageDirectory().getAbsolutePath();//获取SD卡路径
        String path=sd_path+"/Movies/";
        video.setVideoPath(path+"哈利波特与阿兹卡班的囚徒.mp4");
        MediaController mediaController = new MediaController(getContext());
        video.setMediaController(mediaController);
        mediaController.setMediaPlayer(video);

    }
    public  void Full(){

    }


    }





